import java.time.*;
import java.time.format.DateTimeFormatter;

public class TimeConverter {
    private static final long FIVE_MINUTES_IN_MILLIS = 5 * 60 * 1000;
    private static final long ONE_HOUR_IN_MILLIS = 60 * 60 * 1000;
    private static final long TWELVE_HOURS_IN_MILLIS = 12 * 60 * 60 * 1000;
    private static final long ONE_DAY_IN_MILLIS = 24 * 60 * 60 * 1000;
    private static final long TWO_DAYS_IN_MILLIS = 2 * ONE_DAY_IN_MILLIS;

    public static String convertTimestamp(long timestampMillis) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime inputTime = LocalDateTime.ofInstant(
                Instant.ofEpochMilli(timestampMillis),
                ZoneId.systemDefault()
        );

        Duration duration = Duration.between(inputTime, now);
        long millisDiff = duration.toMillis();

        // 5分钟以内
        if (millisDiff < FIVE_MINUTES_IN_MILLIS) {
            return "刚刚";
        }

        // 5分钟至1小时
        if (millisDiff < ONE_HOUR_IN_MILLIS) {
            long minutes = duration.toMinutes();
            return minutes + "分钟前";
        }

        // 1小时至12小时
        if (millisDiff < TWELVE_HOURS_IN_MILLIS) {
            long hours = duration.toHours();
            return hours + "小时前";
        }

        // 获取今天和昨天的0点时间
        LocalDate today = LocalDate.now();
        LocalDateTime todayMidnight = today.atStartOfDay();
        LocalDateTime yesterdayMidnight = todayMidnight.minusDays(1);
        LocalDateTime dayBeforeYesterdayMidnight = todayMidnight.minusDays(2);

        // 12小时至24小时
        if (millisDiff < TWO_DAYS_IN_MILLIS) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
            String timeStr = inputTime.format(formatter);

            if (inputTime.isAfter(todayMidnight)) {
                return "今天 " + timeStr;
            } else if (inputTime.isAfter(yesterdayMidnight)) {
                return "昨天 " + timeStr;
            }
        }

        // 48小时以上
        LocalDate inputDate = inputTime.toLocalDate();
        DateTimeFormatter sameYearFormatter = DateTimeFormatter.ofPattern("M-d");
        DateTimeFormatter diffYearFormatter = DateTimeFormatter.ofPattern("yyyy-M-d");

        if (inputDate.getYear() == today.getYear()) {
            return inputDate.format(sameYearFormatter);
        } else {
            return inputDate.format(diffYearFormatter);
        }
    }

    // 测试方法
    public static void main(String[] args) {
        // 测试用例，基于2025-07-10 21:18 KST
        long now = System.currentTimeMillis();
        System.out.println("刚刚: " + convertTimestamp(now - 2 * 60 * 1000)); // 2分钟前
        System.out.println("分钟前: " + convertTimestamp(now - 30 * 60 * 1000)); // 30分钟前
        System.out.println("小时前: " + convertTimestamp(now - 5 * 60 * 60 * 1000)); // 5小时前
        System.out.println("今天: " + convertTimestamp(now - 15 * 60 * 60 * 1000)); // 15小时前，今天
        System.out.println("昨天: " + convertTimestamp(now - 30 * 60 * 60 * 1000)); // 30小时前，昨天
        System.out.println("同年: " + convertTimestamp(now - 60 * 24 * 60 * 60 * 1000L)); // 60天前 5-11
        System.out.println("不同年: " + convertTimestamp(now - 400 * 24 * 60 * 60 * 1000L)); // 400天前 2024-6-5
    }
}